/**
 * 测试二级分类tabs功能
 * 模拟分类数据结构和功能逻辑
 */

// 模拟分类数据
const mockCategories = [
    {
        id: 1,
        text: '数码产品',
        children: [
            { id: 11, text: '手机' },
            { id: 12, text: '电脑' },
            { id: 13, text: '配件' }
        ]
    },
    {
        id: 2,
        text: '服装鞋帽',
        children: [
            { id: 21, text: '男装' },
            { id: 22, text: '女装' },
            { id: 23, text: '鞋靴' }
        ]
    },
    {
        id: 3,
        text: '食品饮料',
        children: [
            { id: 31, text: '零食' },
            { id: 32, text: '饮料' },
            { id: 33, text: '生鲜' }
        ]
    },
    {
        id: 4,
        text: '家居用品',
        children: []
    }
];

// 模拟页面数据
const mockPageData = {
    items: mockCategories,
    mainActiveIndex: 0,
    subCatsTabs: [],
    activeTabIndex: 0,
    currentCatId: 0,
    currentSubCatId: 0
};

/**
 * 更新二级分类tabs
 */
function updateSubCatsTabs(mainIndex) {
    const category = mockPageData.items[mainIndex];
    let subCatsTabs = [];
    
    if (category && category.children && category.children.length > 0) {
        // 添加"全部"选项
        subCatsTabs.push({
            id: 0,
            title: '全部',
            name: 'all'
        });
        
        // 添加二级分类
        category.children.forEach(child => {
            subCatsTabs.push({
                id: child.id,
                title: child.text,
                name: `sub_${child.id}`
            });
        });
    } else {
        // 如果没有二级分类，只显示"全部"
        subCatsTabs.push({
            id: 0,
            title: '全部',
            name: 'all'
        });
    }
    
    mockPageData.subCatsTabs = subCatsTabs;
    mockPageData.activeTabIndex = 0;
    mockPageData.currentSubCatId = 0;
    
    return subCatsTabs;
}

/**
 * 点击一级分类
 */
function onClickNav(index) {
    const category = mockPageData.items[index];
    
    mockPageData.mainActiveIndex = index;
    mockPageData.currentCatId = category.id;
    mockPageData.currentSubCatId = 0;
    
    // 更新二级分类tabs
    const subCatsTabs = updateSubCatsTabs(index);
    
    console.log(`点击一级分类: ${category.text}`);
    console.log('当前分类ID:', mockPageData.currentCatId);
    console.log('二级分类tabs:', subCatsTabs);
    
    return {
        mainActiveIndex: index,
        currentCatId: category.id,
        subCatsTabs: subCatsTabs
    };
}

/**
 * 点击二级分类tab
 */
function onSubCatTabChange(index) {
    const subCat = mockPageData.subCatsTabs[index];
    
    mockPageData.activeTabIndex = index;
    mockPageData.currentSubCatId = subCat.id;
    
    console.log(`点击二级分类: ${subCat.title}`);
    console.log('当前二级分类ID:', mockPageData.currentSubCatId);
    
    return {
        activeTabIndex: index,
        currentSubCatId: subCat.id
    };
}

// 测试用例
console.log('=== 测试二级分类tabs功能 ===');

// 测试1: 点击"数码产品"分类
console.log('\n测试1: 点击"数码产品"分类');
const result1 = onClickNav(0);
console.log('结果:', result1);

// 测试2: 点击"手机"二级分类
console.log('\n测试2: 点击"手机"二级分类');
const result2 = onSubCatTabChange(1);
console.log('结果:', result2);

// 测试3: 点击"服装鞋帽"分类
console.log('\n测试3: 点击"服装鞋帽"分类');
const result3 = onClickNav(1);
console.log('结果:', result3);

// 测试4: 点击"家居用品"分类（无二级分类）
console.log('\n测试4: 点击"家居用品"分类（无二级分类）');
const result4 = onClickNav(3);
console.log('结果:', result4);

console.log('\n=== 测试完成 ==='); 